package com.muki.extractor;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.muki.contentSeen.Content;

@Component
public class ExtractorBeen {

	private final String urlRegx = "\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))";
	private final Logger log = Logger.getLogger(ExtractorBeen.class);
	
	@Autowired private JmsMessagingTemplate jmsMessagingTemplate;

	/**
	 * 抓取 been
	 * @param been
	 */
	@Async("myTaskAsyncPool")  //myTaskAsynPool即配置线程池的方法名，此处如果不写自定义线程池的方法名，会使用默认的线程池  
	public void doTask(Content been,CountDownLatch latch) throws InterruptedException{  
		String url = been.getUrl().trim();
		String depth = String.valueOf(been.getDepth());
		String context = been.getContent();
		String topic = been.getTopic();

		Pattern pattern = Pattern.compile(urlRegx,
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(context);

		while(matcher.find()) {
			String link=matcher.group().trim();
			link=link.replaceAll("href\\s*=\\s*(['|\"]*)", "");
			link=link.replaceAll("['|\"]", "").trim();
			link=LinkBeenDefinition(link,url);
			Map<String,String> sets = new HashMap<>();
			sets.put("url", url);
			sets.put("link",link);
			sets.put("depth", depth);
			sets.put("topic", topic);
			jmsMessagingTemplate.convertAndSend("urls",sets);
		}
		
		latch.countDown();
	}    

	/**
	 * 
	 * @param uri 原uri
	 * @param link 获取到的链接
	 * @return
	 */
	private String LinkBeenDefinition(String link,String uri) {
		if(link.startsWith("../")) {
			int root = uri.lastIndexOf("/");
			uri = uri.substring(0,root+1);
			link = link.substring(3);
			return LinkBeenDefinition(uri, link);
		} else if(link.startsWith("#") || link.startsWith("\\")) {
			return uri;
		} else if(link.startsWith("/")) {
			int first = uri.indexOf("/",0);
			int se = uri.indexOf("/",first+1);
			int root = uri.indexOf("/",se+1);
			uri = uri.substring(0,root);
			return uri+link;
		} else if(!link.startsWith("http") && !link.startsWith("javascript")) {
			int first = uri.indexOf("/",0);
			int se = uri.indexOf("/",first);
			int last = uri.lastIndexOf("/");
			if(se<last) {
				uri = uri.substring(0,last+1);
				return uri+link;
			} else
				return uri+"/"+link;
		} 
		return link;
	}
}
